package dao.impl;

import dao.BookInfoDao;
import entity.BookInfo;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import util.DbUtils;

import java.sql.SQLException;
import java.util.List;

/**
 * User: key
 * ClassName:Book_InfoDaoImpl
 * Date: 2023/5/6
 * Time: 15:07
 * description:
 */
public class BookInfoDaoImpl implements BookInfoDao {
    private QueryRunner qr = new QueryRunner(DbUtils.getDataSource());
    @Override
    public BookInfo getBookById(int bookId) throws SQLException {
        String sql = "select book_id 'bookId',book_code 'bookCode',book_name 'bookName',book_type 'bookType'," +
                "book_author 'bookAuthor',publish_press 'publishPress',publish_date 'publishDate',is_borrow 'isBorrow' " +
                "from book_info where bookId = ?";
        return qr.query(sql,new BeanHandler<>(BookInfo.class),bookId);
    }

    @Override
    public Long getCount(String countSql, Object[] toArray) throws SQLException {
        return qr.query(countSql,new ScalarHandler<>(),toArray);
    }

    @Override
    public List<BookInfo> getBookByKeyWord(String sql, Object[] toArray) throws SQLException {
        return qr.query(sql,new BeanListHandler<>(BookInfo.class),toArray);
    }
}
